package com.coldwindblows.blog.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.coldwindblows.blog.bo.RestResponseBo;
import com.coldwindblows.blog.constant.WebConst;
import com.coldwindblows.blog.controller.BaseController;
import com.coldwindblows.blog.dto.LogActions;
import com.coldwindblows.blog.dto.Types;
import com.coldwindblows.blog.exception.TipException;
import com.coldwindblows.blog.pojo.ContentDo;
import com.coldwindblows.blog.pojo.MetaDo;
import com.coldwindblows.blog.pojo.UserDo;
import com.coldwindblows.blog.service.IContentService;
import com.coldwindblows.blog.service.ILogService;
import com.coldwindblows.blog.service.IMetaService;
import com.github.pagehelper.PageInfo;

/**
 * Created by ColdWindBlows on 2018/07/06
 *
 */
@Controller
@RequestMapping("/admin/article")
@Transactional(rollbackFor = TipException.class)
@SuppressWarnings("rawtypes")
public class ArticleController extends BaseController {
	
	@Autowired
	private IMetaService metaService;
	
	@Autowired
	private IContentService contentService;
	
	@Autowired
	private ILogService logService;
	
	@GetMapping(value = "")
	public String index(@RequestParam(value = "page", defaultValue = "1")int page, @RequestParam(value = "limit", defaultValue = "15")int limit, Model model) {
		ContentDo content = new ContentDo();
		content.setType(Types.ARTICLE.getType());
		PageInfo<ContentDo> pageList = contentService.getArticlesWithPage(content, page, limit);
		model.addAttribute("articles", pageList);
		
		return "admin/article_list";
	}
	
	@GetMapping(path = "/publish")
	public String newArticle(Model model) {
		List<MetaDo> categories = metaService.getMetas(Types.CATEGORY.getType());
		model.addAttribute("categories", categories);
		
		return "admin/article_edit";
	}
	
	@GetMapping(path = "/{cid}")
	public String editArticle(@PathVariable("cid")String cid, Model model) {
		ContentDo content = contentService.getDetail(cid);
		model.addAttribute("contents", content);
		List<MetaDo> categories = metaService.getMetas(Types.CATEGORY.getType());
		model.addAttribute("categories", categories);
		model.addAttribute("active", "article");
		
		return "admin/article_edit";
	}
	
	@PostMapping("/publish")
	@ResponseBody
	public RestResponseBo publishArticle(ContentDo content, HttpServletRequest request) {
		UserDo user = this.user(request);
		content.setAuthorId(user.getUid());
		content.setType(Types.ARTICLE.getType());
		if(StringUtils.isBlank(content.getCategories())) {
			content.setCategories("默认分类");
		}
		String result = contentService.publish(content);
		if(!WebConst.SUCCESS_RESULT.equals(result)) {
			return RestResponseBo.fail(result);
		}
		
		return RestResponseBo.ok();
	}
	
	@PostMapping("/modify")
	@ResponseBody
	public RestResponseBo modifyArticle(ContentDo content, HttpServletRequest request) {
		UserDo user = this.user(request);
		content.setAuthorId(user.getUid());
		content.setType(Types.ARTICLE.getType());
		String result = contentService.updateArticle(content);
		if(!WebConst.SUCCESS_RESULT.equals(result)) {
			return RestResponseBo.fail(result);
		}
		return RestResponseBo.ok();
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public RestResponseBo delete(int cid, HttpServletRequest request) {
		String result = contentService.deleteContent(cid);
		logService.insertLog(LogActions.DEL_ARTICLE.getAction(), cid + "", request.getRemoteAddr(), this.getUid(request));
		if(!WebConst.SUCCESS_RESULT.equals(result)) {
			return RestResponseBo.fail(result);
		}
		return RestResponseBo.ok();
		
	}
}
